package com.kalvan.client.context;

import org.springframework.core.NamedThreadLocal;

/**
 * context holder
 *
 * @author chenliang
 */
public class RequestContextHolder {
    private static final ThreadLocal<RequestContext> REQUEST_CONTEXT = new NamedThreadLocal<>(
            "request_context");

    public static RequestContext getContext() {
        if (REQUEST_CONTEXT.get() == null) {
            setContext(new RequestContext());
        }
        return REQUEST_CONTEXT.get();
    }

    public static void setContext(RequestContext context) {
        if (context == null) {
            REQUEST_CONTEXT.remove();
        } else {
            REQUEST_CONTEXT.set(context);
        }
    }

    public static void remove() {
        REQUEST_CONTEXT.remove();
    }

}
